package com.dongdong.controller;

import com.dongdong.em.Result;
import com.dongdong.model.TbBrand;
import com.dongdong.page.PageParams;
import com.dongdong.po.BrandPO;
import com.dongdong.service.TbBrandService;
import com.dongdong.vo.BrandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
@RefreshScope       //nacos配置文件热更新   作用：不需要重启服务即可更新
public class TbBrandController {

    @Autowired
    private TbBrandService brandService;

    //查询全部
    @PostMapping("brandList")   //为什么使用post   原因:因为要接收json类型的数据
    public Result getBranList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize) {
        PageParams<TbBrand> brandList = brandService.getBranList(pageSize, pageNum);
        return Result.OK().setData(brandList);
    }
    //查询全部
//    @PostMapping("brandList")   //为什么使用post   原因:因为要接收json类型的数据
//    public Result getBranList() {
//        List<BrandVO> brandList = brandService.getBranList();
//        return Result.OK().setData(brandList);
//    }

    //根据名称模糊查询And根据首字母模糊查询
    @PostMapping("findBrandList")
    public Result findBrandList(@RequestBody BrandVO brandVO) {
        List<BrandVO> brandVOList = brandService.findBrandList(brandVO);
        return Result.OK().setData(brandVOList);
    }

    //增加
    @PostMapping("insertBrand")
    public Result insertBrand(@RequestBody BrandPO brandPO) {
        int i = brandService.insertBrand(brandPO);
        if (i > 0) {
            return Result.OK();
        }
        return Result.ERROR();
    }

    //批量删除
    @DeleteMapping("deleteBrand/{arr}")
    public Result deleteBrand(@PathVariable Long[] arr) {
        int i = brandService.delteBrandAll(arr);
        if (i < 0) {
            return Result.ERROR();
        }
        return Result.OK();
    }

    //回显
    @GetMapping("getBrandById")
    public Result getBrandById(@RequestParam("id") Long id) {
        BrandVO vo = brandService.getBrandById(id);
        return Result.OK().setData(vo);
    }

    //修改
    @PutMapping("updateBrandList")
    public Result updateBrandList(@RequestBody BrandPO brandPO) {
        System.out.println(brandPO);
        int i = brandService.updateBrandList(brandPO);
        if (i < 0) {
            return Result.ERROR();
        }
        return Result.OK();
    }

    //获取select2
    @PostMapping("getBrandListAndSelect2")
    public Result getBrandListAndSelect2(){
        List<BrandVO> brandVOList=brandService.getBrandListAndSelect2();
        return Result.OK().setData(brandVOList);
    }
}
